package com.bbx.taxi.client.Task;

import android.app.Activity;
import android.os.AsyncTask;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.base.BaseRequest;
import com.bbx.api.sdk.model.passanger.WeightOrVolumnBuild;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.CancelReasonListNet;
import com.bbx.api.sdk.net.passeger.conn.WeightNet;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Task.DetailsOfGoodsTask.OnFinishListener;

public class CancelReasonListTask {
	private Activity context;
	private MyApplication mApplication;
	public static CancelReasonListTask instance;
	public CancelReasonTask mCancelReasonTask;
	public interface OnFinishListener {
		public void onFinish();
	}

	static OnFinishListener mOnFinishListener = null;

	public static void setOnFinishListener(OnFinishListener l) {
		mOnFinishListener = l;
	}
	public static CancelReasonListTask getInstance(Activity context,
			MyApplication mApplication) {
		if (instance == null) {
			instance = new CancelReasonListTask(context, mApplication);
			return instance;

		} else {
			if (mOnFinishListener != null) {
				mOnFinishListener.onFinish();
			}
			return instance;
		}
	}
	public CancelReasonListTask(Activity context, MyApplication mApplication) {
		this.context = context;
		this.mApplication = mApplication;
		getCancelList();
	}
	/**
	 * 获取原因列表
	 */
	public void getCancelList(){
		BaseRequest mBaseRequest = new BaseRequest(context);
		mBaseRequest.uid = mApplication.getUid();
		mBaseRequest.access_token = mApplication.getToken();
		mCancelReasonTask = new CancelReasonTask(context, mBaseRequest);
		mCancelReasonTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	/**
	 * 取消原因列表
	 */
	private class  CancelReasonTask extends BaseAsyncTask {
		private BaseRequest mBaseRequest;

		public CancelReasonTask(Activity context,
				BaseRequest mBaseRequest) {
			super(context);
			setIsReturnString(true);
			this.mBaseRequest = mBaseRequest;
		}

		@Override
		public void onSuccess(Object object) {
			// TODO Auto-generated method stub
			if (object != null) {
				SharedPreUtil.putStringValue(context,
						SharedPreEncryptUtil.cancel_reason_json, (String) object);
			}
			if (mOnFinishListener != null) {
				mOnFinishListener.onFinish();
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context,
					context.getResources().getString(R.string.no_network));
			if (mOnFinishListener != null) {
				mOnFinishListener.onFinish();
			}
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mBaseRequest)
					.getJson();
			return new CancelReasonListNet(context, json);
		}

	}
}
